

package search.util;

class LexToken{}
class GraphDefinition extends LexToken{ public String toString(){return "GraphDefinition";}}
class Str extends LexToken{ 
	public String string; 
	public Str(s){ string = s;} 
	public String toString(){ return "STR <" + string + ">";}
}
class Sep extends LexToken{ 
	public String sep; 
	public Sep(x){ sep = x;} 
	public String toString(){ return "SEP " + sep;}
}
class Arrow extends LexToken { public String toString(){return "Arrow";}}
class LexError extends LexToken{
	public LexError(String p){
		println p;
		System.exit(-1);
	}
}


public class TreeParser{


	


	def isWhitespace = {x -> (x as String) in ["\n", "\t", " "] }
	def isSep = {x -> (x as String) in ['[',']','{','}','"','\'','=',',']}
	def isAlphaNum = {x -> (x as String) <= "z" && x >= "0"}
	def isArrow = {x -> x == "->"}



	def lex(input){

		def ret = []

		while(!input.isEmpty()){


			def c = input.remove(0);

			if(isWhitespace (c) || c == ";"){
				//NOP 
			}else if(isAlphaNum(c)){
				def string = "$c" 
				while(isAlphaNum(input[0])){
					string += input[0];
					def ch = input.remove(0);
				}


				if(string == "digraph"){
					ret.add(new GraphDefinition());
				}else{
					ret.add(new Str(string ) );
				}
			}else if(input.size() > 0 && c + input[0] == "->"){
				input.remove(0);
				ret.add(new Arrow());
			}else if(isSep(c)){
				ret.add(new Sep(c));
			}else{
				ret.add(new Str(c));
			}

		}
		return ret;
	}


	def takeWhile(closure, list){



		def counter= list.findIndexOf{ !closure(it)};

		if(counter == -1) return []

		return list[0..(counter - 1)]
	}

	def dropWhile(closure, list){


		def counter= list.findIndexOf{ !closure(it)};

		if(counter == -1) return list

		return list[(counter)..-1]
	}

	public static void main(String[] args){
		

		
    	def input = new File(args[0]);

    	def textin = input.getText().toList();



    	println new TreeParser().lex(textin);

	}


}